/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account.presenter;

import org.zhiqim.account.dao.UserTradeDao;
import org.zhiqim.account.dbo.user.UserInfo;
import org.zhiqim.account.dbo.user.UserTrade;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.validate.ones.IsNumericLen;
import org.zhiqim.httpd.validate.onex.IsAmount2R;
import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.util.Amounts;
import org.zhiqim.orm.ZTable;

import com.alipay.api.AlipayConstants;

/**
 * 用户交易操作处理器
 *
 * @version v1.0.0 @author huangsufang 2018-3-14 新建与整理
 */
@AnAlias("UserTradePresenter")
public class UserTradePresenter implements AlipayConstants
{
    /**
     * 管理台支付宝退款
     * 
     * @param request       请求
     * @throws Exception    异常
     */
    public static void doRefundAlipay(HttpRequest request) throws Exception
    {
        //1.检查参数
        request.addValidate(new IsNumericLen("tradeId", "交易编号不能为空", 16, 16));
        request.addValidate(new IsAmount2R("refundAmount", "退款金额不正确"));
        if (!request.chkValidate())
        {
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        long tradeId = request.getParameterLong("tradeId");
        int refundAmount = request.getParameterAmount2R("refundAmount");
        
        UserTrade trade  = Global.get(ZTable.class).item(UserTrade.class, tradeId);
        if (trade == null)
        {
            request.setResponseError("交易不存在");
            return;
        }
        
        UserInfo user = Global.get(ZTable.class).item(UserInfo.class, trade.getUserId());
        if (user == null)
        {
            request.setResponseError("用户不存在");
            return;
        }
        
        //2.检查金额
        if (refundAmount > trade.getTradeAmount() - trade.getTradeData())
        {
            request.setResponseError("退款金额超出允许值["+Amounts.toYuan(trade.getTradeAmount() - trade.getTradeData())+"]元");
            return;
        }
        
        if (refundAmount > user.getUserBalance())
        {
            request.setResponseError("退款金额超出用户余额");
            return;
        }
        
        //3.调用退款操作
        String result = UserTradeDao.doRefund(trade, user, refundAmount);
        if (result != null)
            request.setResponseError(result);
        else
            request.setResponseResult("退款成功");
    }
}
